package org.openea.eap.module.obpm.controller.custsql;

import com.alibaba.fastjson.JSONObject;
import org.openbpm.base.api.aop.annotion.CatchErr;
import org.openbpm.base.api.response.impl.ResultMsg;
import org.openbpm.base.core.util.StringUtil;
import org.openea.eap.module.obpm.service.custsql.FormCustSqlViewManager;
import org.openea.eap.module.obpm.model.custsql.IdRequest;
import org.openea.eap.module.obpm.model.custsql.SqlViewRequest;
import org.openea.eap.module.obpm.model.custsql.vo.SqlViewDataVO;
import org.openea.eap.module.obpm.model.custsql.vo.SqlViewVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;


@RestController
@RequestMapping({"/form/formCustSql/view"})
public class FormCustSqlViewController {
    protected Logger logger = LoggerFactory.getLogger(getClass());


    @Resource
    FormCustSqlViewManager formCustSqlViewManager;


    @RequestMapping({"/vo_{code}"})
    @CatchErr
    // FormCustSql => JSONObject
    public ResultMsg<JSONObject> getVo(@PathVariable("code") String code) {
        this.logger.info("根据别名获取自定义sql对象,code:{}", code);
        return this.formCustSqlViewManager.getVoJson(code);
    }

    @RequestMapping({"/vo2_{code}"})
    @CatchErr
    public ResultMsg<JSONObject> getVoJson(@PathVariable("code") String code) {
        this.logger.info("根据别名获取自定义sql对象,code:{}", code);
        return this.formCustSqlViewManager.getVoJson(code);
    }


    @RequestMapping({"/list_{code}"})
    @ResponseBody
    @CatchErr
    public ResultMsg<SqlViewDataVO> listData(@PathVariable("code") String code, @RequestParam(value = "bpmExt", required = false) String bpmExt, @RequestBody SqlViewRequest request) {
        this.logger.info("自定义列表-数据查询,code:{}", code);

        if (StringUtil.isEmpty(code)) {
            return ResultMsg.ERROR("编码不能为空");
        }

        request.setCode(code);

        if (StringUtil.isNotEmpty(bpmExt)) {
            request.setBpmExt(bpmExt);
        }

        return this.formCustSqlViewManager.listData(request);
    }


    @RequestMapping({"/sql_{code}"})
    @ResponseBody
    @CatchErr
    public ResultMsg<SqlViewVO> listDataSql(@PathVariable("code") String code, @RequestBody SqlViewRequest request) {
        this.logger.info("自定义列表-数据查询,code:{}", code);

        if (StringUtil.isEmpty(code)) {
            return ResultMsg.ERROR("编码不能为空");
        }

        request.setCode(code);

        return this.formCustSqlViewManager.listDataSql(request);
    }


    /**
     * 导出列表数据
     * @param code
     * @param request
     * @param response
     * @return
     */
    @RequestMapping({"/export_{code}"})
    public ResultMsg<String> export(@PathVariable("code") String code, SqlViewRequest request, HttpServletResponse response) {
        if (StringUtil.isEmpty(code)) {
            this.logger.error("编码不能为空");
            return ResultMsg.ERROR("编码不能为空");
        }
        request.setCode(code);
        return this.formCustSqlViewManager.exportData(request, response);
    }


    @RequestMapping({"/changeData_{code}_{buttonAlias}"})
    public ResultMsg<String> changeData(@PathVariable("code") String code, @PathVariable("buttonAlias") String buttonAlias, @RequestBody IdRequest request) {
        if (StringUtil.isEmpty(code)) {
            this.logger.error("编码不能为空");
            return ResultMsg.ERROR("编码不能为空");
        }

        if (StringUtil.isEmpty(buttonAlias)) {
            this.logger.error("按钮别名不能为空");
            return ResultMsg.ERROR("按钮别名不能为空");
        }

        String id = request.getId();
        if (StringUtil.isEmpty(id)) {
            this.logger.error("ID不能为空");
            return ResultMsg.ERROR("ID不能为空");
        }

        return this.formCustSqlViewManager.changeData(code, buttonAlias, id);
    }
}
